package com.neusoft.shopping.manager.controller;

import com.neusoft.shopping.manager.entity.StockEntity;
import com.neusoft.shopping.manager.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StockController {
    @Autowired
    private StockService service;
    @RequestMapping("/stock_show")
    public ModelAndView getStock(){
        ModelAndView modelAndView=new ModelAndView();
        List<StockEntity> stocks=service.qureyAll();
        modelAndView.addObject("stocks",stocks);
        modelAndView.setViewName("stock_show");
        return modelAndView;
    }

    @RequestMapping("/stockModify/{method}/{stockno}")
    public ModelAndView stockModify(@PathVariable("method") String method, @PathVariable("stockno") Integer stockno){
        ModelAndView modelAndView=new ModelAndView();
        if(method.equals("update")){
            StockEntity stock=service.qureyOne(stockno);
            modelAndView.addObject("stock",stock);
            modelAndView.setViewName("stock_modify");
            return modelAndView;
        }
        else if (method.equals("add")){
            modelAndView.setViewName("stock_modify");
            return modelAndView;
        }else {
            service.del_stock(stockno);
            return this.getStock();
        }
    }

    @RequestMapping("/stockModifyConserve")
    public ModelAndView stockModifyConserve(@RequestParam("method") String method , StockEntity stock){
        if(method.equals("add")){
            service.add_stock(stock);
        }
        if(method.equals("update")){
            service.update_stock(stock);
        }
        return this.getStock();
    }

}
